package ctrlLayer;
import java.util.ArrayList;

import dbLayer.DbPartner;
import modelLayer.BusinessPartner;
import modelLayer.Customer;
import modelLayer.Supplier;
public class CtrBPartner {
	
	DbPartner dbp;
	
	/**
	 * @param dbp
	 */
	public CtrBPartner() {
		dbp = new DbPartner();
	}
	
	/**
	 * Method for retrieving a specific customer.
	 * @param customerId The id of the customer.
	 * @return A customer if found, otherwise null.
	 */
	public Customer retrieveCustomer(int customerId){
		return dbp.retrieveCustomerFromDb(customerId);
	}
	
	/**
	 * Method for retrieving a specific Supplier.
	 * @param supplierId The id of the suplier
	 * @return A supplier if found, otherwise null.
	 */
	public Supplier retrieveSupplier(int supplierId){
		return dbp.retrieveSupplierFromDb(supplierId);
	}
	
	/**
	 * Method for retrieving all customers.
	 * @return A list of all customers, empty if none is in the DB.
	 */
	public ArrayList<Customer> retrieveAllCustomers(){
		return dbp.retrieveAllCustomers();
	}
	
	/**
	 * Method for retrieving all suppliers.
	 * @return A list of all suppliers, empty if none is in the DB.
	 */
	public ArrayList<Supplier> retrieveAllSuppliers(){
		return dbp.retrieveAllSuppliers();
	}
	
	/**
	 * Method for retrieving a list of customers with a name containing the parameter string
	 * @param name The string which has to be contained in their names.
	 * @return A list of the found customers, empty if none is found.
	 */
	public ArrayList<Customer> retrieveCustomerByName(String name){
		return dbp.retrieveCustomerByName(name);
	}
	
	/**
	 * Method for retrieving a list of suppliers with a name containing the parameter string
	 * @param name The String which has to be contained in their names.
	 * @return A list of the found suppliers, empty if none is found.
	 */
	public ArrayList<Supplier> retrieveSupplierByName(String name){
		return dbp.retrieveSupplierByName(name);
	}
	
	public Customer createCustomer(String name, String address, String email, String phoneNo,
			int zipCode, boolean isPrivate){
		Customer cusObj = new Customer(name, address, email, phoneNo, zipCode, isPrivate);
		try {
			dbp.insertBusinessPartner(cusObj);
			ArrayList<Customer> cusList = dbp.retrieveAllCustomers();
			int val = cusList.size() - 1;
			cusObj = cusList.get(val);
		} catch (Exception e) {
			System.out.println("Error in createCustomer in CtrBPartner!");
		}
		return cusObj;
	}
	
}
